import unittest, requests


class WeiDuDianShang(unittest.TestCase):
    url = 'http://10.59.9.18'

    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    # 登录
    userId = None
    sessionId = None
    message = None
    nickName = None
    balance = None

    def test_mall_01(self):
        r1 = requests.post(url=WeiDuDianShang.url + '/small/user/v1/login',
                           json={"phone": "15535738398", "pwd": "zjj031613"})
        print(r1.json())
        # 提取用户id
        WeiDuDianShang.userId = r1.json()['result']['userId']
        print(WeiDuDianShang.userId)
        # 提取sessionId
        WeiDuDianShang.sessionId = r1.json()['result']['sessionId']
        print(WeiDuDianShang.sessionId)
        # 提取响应信息中的message信息
        WeiDuDianShang.message = r1.json()['message']
        # 响应断言
        self.assertEqual('登录成功', WeiDuDianShang.message)
        self.assertIn('成功', WeiDuDianShang.message)
        self.assertTrue('登录成功' == WeiDuDianShang.message)


    # 修改昵称
    message2 = None

    def test_mall_02(self):
        r2 = requests.put(url=WeiDuDianShang.url + '/small/user/verify/v1/modifyUserNick',
                          headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                          params={"nickName": "冬天A"})
        print(r2.json())

    # 新增收货地址
    message3 = None

    def test_mall_03(self):
        r3 = requests.post(url=WeiDuDianShang.url + '/small/user/verify/v1/addReceiveAddress',
                           headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                           params={"realName": '小刘', "phone": "18185603008",'address':'北京 海淀区 八维',
                                   "zipCode": '101010'})
        print(r3.json())
        WeiDuDianShang.message3 = r3.json()['message']
        print(WeiDuDianShang.message3)


    # 收货地址列表
    address_id = None

    def test_mall_04(self):
        r4 = requests.get(url=WeiDuDianShang.url + '/small/user/verify/v1/receiveAddressList',
                          headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)})
        print(r4.json())
        WeiDuDianShang.address_id = r4.json()['result'][0]['id']
        print(WeiDuDianShang.address_id)
        self.assertTrue(7859 == WeiDuDianShang.address_id)
        self.assertTrue(7859, WeiDuDianShang.address_id)

    # 设置默认收货地址
    message5 = None

    def test_mall_05(self):
        r5 = requests.post(url=WeiDuDianShang.url + '/small/user/verify/v1/setDefaultReceiveAddress',
                           headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                           params={"id": WeiDuDianShang.address_id})
        print(r5.json())
        WeiDuDianShang.message5 = r5.json()['message']
        self.assertIn('成功', WeiDuDianShang.message5)
        self.assertTrue('设置成功', WeiDuDianShang.message5)
        self.assertEqual('设置成功', WeiDuDianShang.message5)

    # 查询购物车
    categoryName = None

    def test_mall_06(self):
        r6 = requests.get(url=WeiDuDianShang.url + '/small/order/verify/v1/findShoppingCart',
                          headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)})
        print(r6.json())
        WeiDuDianShang.categoryName = r6.json()['result'][0]['categoryName']
        print(WeiDuDianShang.categoryName)
        self.assertIn('美妆', WeiDuDianShang.categoryName)
        self.assertEqual('美妆护肤', WeiDuDianShang.categoryName)
        self.assertTrue('美妆护肤' == WeiDuDianShang.categoryName)

    # 创建订单
    orderId = None
    message7 = None

    def test_mall_07(self):
        r7 = requests.post(url=WeiDuDianShang.url + '/small/order/verify/v1/createOrder',
                           headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                           params={"orderInfo": "[{'commodityId':23,'amount':1}]",
                                   "totalPrice": 139.00, "addressId": WeiDuDianShang.address_id})
        print(r7.json())
        WeiDuDianShang.orderId = r7.json()['orderId']
        print(WeiDuDianShang.orderId)
        WeiDuDianShang.message7 = r7.json()['message']
        print(WeiDuDianShang.message7)
        self.assertIn('成功', WeiDuDianShang.message7)
        self.assertEqual('创建订单成功', WeiDuDianShang.message7)
        self.assertTrue('创建订单成功' == WeiDuDianShang.message7)

    # 支付
    message8 = None

    def test_mall_08(self):
        r8 = requests.post(url=WeiDuDianShang.url + '/small/order/verify/v1/pay',
                           headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                           params={"orderId": WeiDuDianShang.orderId,
                                   "payType": 1})
        print(r8.json())
        WeiDuDianShang.message8 = r8.json()['message']
        print(WeiDuDianShang.message8)
        self.assertIn('成功', WeiDuDianShang.message8)
        self.assertTrue('支付成功' == WeiDuDianShang.message8)
        self.assertEqual('支付成功', WeiDuDianShang.message8)

    # 查询订单明细数据
    # 商品名称
    commodityName = None
    commodityPrice = None

    def test_mall_09(self):
        r9 = requests.get(url=WeiDuDianShang.url + '/small/order/verify/v1/findOrderInfo',
                          headers={"userId": str(WeiDuDianShang.userId), "sessionId": str(WeiDuDianShang.sessionId)},
                          params={"orderId": WeiDuDianShang.orderId})
        print(r9.json())


if __name__ == '__main__':
    unittest.main()
